﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook
{
    public partial class Menu_Right : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string CaId = Request.QueryString["Product"];
                string CatId = "";
                BindListProduct(CaId);
                //LoadMenu();
                List<Data.ProductsInfo> litsDemo = Business.ProductsService.Products_GetByCaId(CaId);
                if (Business.ProductsService.Products_GetByCaId(CaId).Count > 0)
                {
                    CatId = Business.ProductsService.Products_GetByCaId(CaId)[0].CatId;
                }
                else
                    CatId = "0";
                List<Data.CategoryInfo> listC = Business.CategoryService.Category_GetById(CatId);
                if (listC.Count > 0)
                {
                    CatName = Business.CategoryService.Category_GetById(CatId)[0].cName;
                }
                else
                    CatName = "Chưa có";
                
            }
            else
                return;
        }
        protected string CatName = "";
        protected string PUName = "";
        //Load Products
        //protected void LoadData(string request)
        //{
        //    rptProducts.DataSource = Business.ProductsService.Products_GetByCaId(request);
        //    rptProducts.DataBind();
        //}
        //Load du lieu ra Web
      //Phan trang du lieu 
        private void BindListProduct( string request)
        {
            int pageSize = 10; // so san pham tren 1 trang
            int total = 0;
            total = Business.ProductsService.Products_GetByCaId(request).Count;
            int pages = total / pageSize; // so trang
            if (total % pageSize > 0)
                pages++;
            int currentPage = 1; // trang hien tai
            if (Request.QueryString["page"] != null)
            {
                try
                {
                    currentPage = Convert.ToInt32(Request.QueryString["page"]);
                }
                catch
                {
                    Response.Redirect("Menu_Right.aspx?Product="+request);
                    currentPage = 1;
                }
            }
            if (currentPage <= 1)
            {
                ltrPaging.Text = "<span class=\"disabled\"><< prev</span>";
            }
            else
            {
                ltrPaging.Text = "<a href=\"Menu_Right.aspx?Product="+request+"&page=" + (currentPage - 1) + "\"><< prev</a>";
            }
            for (int i = 1; i <= pages; i++)
            {
                if ((i > 3) && (currentPage - i > 3 && currentPage - i < 10 || (currentPage - i > 12)) || (i - currentPage > 3 && i - currentPage < 10 || (i - currentPage > 12)) && i < pages - 2)
                {
                    continue;
                }
                if (currentPage == i)
                    ltrPaging.Text += "<span class=\"current\">" + i + "</span>";
                else
                {
                    ltrPaging.Text += "<a href=\"Menu_Right.aspx?Product="+request +"&page=" + i + "\">" + i + "</a>";
                }
            }
            if (currentPage >= pages)
            {
                ltrPaging.Text += "<span class=\"disabled\">next >></span>";
                //Response.Redirect("/AdminIstrator/ListDelivery.aspx?page=4");
            }
            else
            {
                ltrPaging.Text += "<a href=\"Menu_Right.aspx?Product="+request +"&page=" + (currentPage + 1) + "\">next >></a>";
            }
            if (currentPage <= 0)
            {
                currentPage = 1;
                Response.Redirect("Menu_Right.aspx?Product="+request);
            }
            rptProducts.DataSource = Business.ProductsService.Products_GetByCatId_Paging(request, currentPage.ToString(), pageSize.ToString());

            rptProducts.DataBind();
        }
    }
}